package com.zhku.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.user.mapper.TtUserRoleMapper;
import com.zhku.user.model.dto.UpdateUserRoleDto;
import com.zhku.user.model.po.TtUserRole;
import com.zhku.user.service.TtUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TtUserRoleServiceImpl extends ServiceImpl<TtUserRoleMapper, TtUserRole> implements TtUserRoleService {

    @Autowired
    TtUserRoleMapper ttUserRoleMapper;

    @Override
    public void updateUserRole(UpdateUserRoleDto updateUserRoleDto) {
        String roleId = updateUserRoleDto.getRoleId();
        String userId = updateUserRoleDto.getUserId();
        if (StringUtils.isEmpty(roleId) || StringUtils.isEmpty(userId)){
            throw new TaoTaoException("传入的用户id或指定的角色编号为空");
        }
        TtUserRole ttUserRole = ttUserRoleMapper.selectOne(new LambdaQueryWrapper<TtUserRole>().eq(TtUserRole::getUserId, userId));
        if (ttUserRole == null) {
            throw new TaoTaoException("当前用户没有角色关联表");
        }
        ttUserRole.setRoleId(roleId);
        int i = ttUserRoleMapper.updateById(ttUserRole);
        if (i <= 0) {
            throw new TaoTaoException("更新用户角色失败");
        }
    }
}
